﻿using Manage.Traceability.Api.DTO;
using Manage.Traceability.Domain;
using Manage.Traceability.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Manage.Traceability.Api.Controllers
{
    /// <summary>
    /// 养殖控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FarmingController : ControllerBase
    {
        private readonly ISqlSugarClient db;

        public FarmingController(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 显示养殖信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<FarmingDTO> ShowFarming()
        {
            var list = db.Queryable<FarmingModel>().Where(x => x.Isdelete == false)
                .Select(x => new FarmingDTO
                {
                    FarmingId = x.FarmingId,
                    FarmingName = x.FarmingName,
                    FarmingFrom = x.FarmingFrom,
                    FarmingDay = x.FarmingDay,
                    FarmingBrand = x.FarmingBrand,
                    FarmingNickName = x.FarmingNickName,
                    FarmingTime = x.FarmingTime,
                }).OrderByDescending(x => x.FarmingId).Take(1).First();
            return ApiResult<FarmingDTO>.Success("显示养殖信息", list);
        }
        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddFarming(FarmingDTO dto)
        {
            FarmingModel farming = new FarmingModel();
            farming.FarmingName = dto.FarmingName;
            farming.FarmingFrom = dto.FarmingFrom;
            farming.FarmingDay = dto.FarmingDay;
            farming.FarmingBrand = dto.FarmingBrand;
            farming.FarmingNickName = dto.FarmingNickName;
            farming.FarmingTime = dto.FarmingTime;
            int r1 = db.Insertable(farming).ExecuteCommand(); //都是参数化实现
            if (r1 > 0)
            {
                return ApiResult<int>.Success("添加养殖信息成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加养殖信息失败");
            }
        }
        /// <summary>
        /// 显示企业养殖信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<FarmingCompanyDTO> ShowFarmingCompany()
        {
            var list = db.Queryable<FarmingCompanyModel>().Where(x => x.CompanyIsdelete == false)
               .Select(x => new FarmingCompanyDTO
               {
                   CompanyId = x.CompanyId,
                   CompanyName = x.CompanyName,
                   CompanyUserName = x.CompanyUserName,
                   CompanyCount = x.CompanyCount,
               }).OrderByDescending(x => x.CompanyId).Take(1).First();
            return ApiResult<FarmingCompanyDTO>.Success("显示养殖企业信息", list);
        }
        /// <summary>
        /// 添加养殖企业信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddFarmingCompany(FarmingCompanyDTO dto)
        {
            FarmingCompanyModel fcom = new FarmingCompanyModel();
            fcom.CompanyName = dto.CompanyName;
            fcom.CompanyUserName = dto.CompanyUserName;
            fcom.CompanyCount = dto.CompanyCount;
            int r1 = db.Insertable(fcom).ExecuteCommand(); //都是参数化实现
            if (r1 > 0)
            {
                return ApiResult<int>.Success("添加养殖企业信息成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加养殖企业信息失败");
            }
        }
        /// <summary>
        /// 显示保健信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<HealthDTO>> ShowHealth()
        {
            var list = db.Queryable<HealthModel>().Where(x => x.HealthIsdelete == false)
               .Select(x => new HealthDTO
               {
                   HealthId = x.HealthId,
                   HealthDay = x.HealthDay,
                   HealthTypeName = x.HealthTypeName,
                   HealthName = x.HealthName,
               }).ToList();
            return ApiResult<List<HealthDTO>>.Success("显示养殖企业信息", list);
        }
        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddHealth(HealthDTO dto)
        {
            HealthModel health = new HealthModel();
            health.HealthName = dto.HealthName;
            health.HealthTypeName = dto.HealthTypeName;
            health.HealthDay = dto.HealthDay;
            int r1 = db.Insertable(health).ExecuteCommand(); //都是参数化实现
            if (r1 > 0)
            {
                return ApiResult<int>.Success("添加保健信息成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加保健信息失败");

            }
        }
        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> UpdateSource(HealthModel health)
        {
            //根据主键更新单条 参数 Class
            int result = db.Updateable(health).Where(x => x.HealthId == health.HealthId).ExecuteCommand();//实体有多少列更新多少列
            if (result > 0)
            {
                return ApiResult<int>.Success("修改成功", result);
            }
            else
            {
                return ApiResult<int>.Fail("修改失败");
            }
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int>Delhealth(int id)
        {
            int r1 = db.Deleteable<HealthModel>().In(id).ExecuteCommand();
            if (r1 > 0)
            {
                return ApiResult<int>.Success("删除成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("删除失败");
            }
        }
    }
}